package fizzpicks.tetrapulp.tetrapulp;

import pulpcore.animation.Fixed;
import pulpcore.sound.Sound;

import java.util.Set;

/**
 * Created by will
 * Date: May 3, 2008 8:16:25 PM
 */
public class PCSound extends LJMusicPlay {

    static boolean withSound = false;

    static final int shiftPitches[] = {
            630, 667, 707, 749, 794, 841, 891, 944,
            1000, 1059, 1122, 1189, 1260, 1335, 1424, 1498
    };

    /**
     * Plays a sample from datafile by name, with the specified volume
     * and the specified rate scale factor, panned center, without loop.
     *
     * @param filename name of object in datafile
     * @param vol      scale factor for volume (0-255); if set to 0,
     *                 stops all voices playing the sample
     * @param pitch    scale factor for playback rate (1000 = normal; 2000 = double speed)
     */
    static void playPitchSample(String filename, int vol, int pitch) {
        if (withSound) {
//            InputStream entry = Assets.getAsStream(filename);
            Sound entry = Sound.load(filename);
            if (entry != null) {
                if (vol < 1) {
                    //stop_sample(entry.dat);
                } else {
                    entry.play(new Fixed(vol / 255));
                }
            }
        }
    }

    static void ezPlaySample(String filename, int vol) {
        playPitchSample(filename, vol, 1000);
    }

    static final char tetrominoNames[] = "IJLOSTZ22V".toCharArray();

    void playSampleForTetromino(LJP piece) {
//  piece &= LJP.MASK.index;
//  if (piece >= 0 && piece < 10) {
        String filename = "next" + tetrominoNames[piece.index] + "_wav";
        ezPlaySample(filename, 128);
//  }
    }

    void playSoundEffects(LJView v, Set<LJSND> sounds, int countdown) {
        // Handle sound
        if ((sounds.contains(LJSND.SPAWN))
                && !(v.hideNext)) {
            playSampleForTetromino(v.field.curPiece[1]);
        }
        if (sounds.contains(LJSND.HOLD)) {
            ezPlaySample("hold_wav", 128);
        }
        if (sounds.contains(LJSND.ROTATE)) {
            ezPlaySample("rotate_wav", 128);
        }
        if (sounds.contains(LJSND.IRS)) {
            ezPlaySample("irs_wav", 128);
        }
        if (sounds.contains(LJSND.SQUARE)) {
            ezPlaySample("square_wav", 128);
        }
        if (sounds.contains(LJSND.SHIFT)) {
            int x = v.plat.skin.shiftScale
                    ? v.field.x + 8 - (LJ_PF_WID - 4) / 2
                    : 8;
            int pitch = shiftPitches[x];
            playPitchSample("shift_wav", 128, pitch);
        }
        if (sounds.contains(LJSND.LAND)) {
            ezPlaySample("land_wav", 192);
        }
        if (sounds.contains(LJSND.LOCK)) {
            ezPlaySample("lock_wav", 192);
        }
        if (sounds.contains(LJSND.LINE)) {
            ezPlaySample("line_wav", 128);
        }
        if (sounds.contains(LJSND.SECTIONUP)) {
            ezPlaySample("sectionup_wav", 128);
        }

        if (v.plat.b2bcd1 > 0) {
            if (--v.plat.b2bcd1 == 0) {
                playPitchSample("line_wav", 148, 1200);
            }
        }
        if (sounds.contains(LJSND.SETB2B)) {
            v.plat.b2bcd1 = 10;
        }
        if (v.plat.b2bcd2 > 0) {
            if (--v.plat.b2bcd2 == 0) {
                playPitchSample("line_wav", 168, 1440);
            }
        }
        if (sounds.contains(LJSND.B2B)) {
            v.plat.b2bcd2 = 20;
        }
        if (countdown < v.control.countdown) {
            String name;
            name = "count_" + countdown + "_wav";
            ezPlaySample(name, 128);
        }
//        LJMusic_poll(v.plat.skin.bgm);
    }


}
